package targaman.moltenwow.autovoter.components;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import targaman.moltenwow.autovoter.Logger.MoltenLogger;
import targaman.moltenwow.autovoter.lang.Constants;
import targaman.moltenwow.autovoter.services.VerifyInternetConnectivity;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by Jonathan on 26/12/2014.
 */
@Component(value = Constants.VERIFY_INTERNET_CONNETIVITY_SERVICE)
public class VerifyInternetConnectivityImpl implements VerifyInternetConnectivity {

   private InetAddress address;


    @Autowired
    @Qualifier(value = Constants.TEST_HOST_NAME)
    private String testHostname;


    @Override
    public boolean isConnected() {

        try {
            address = InetAddress.getByName(testHostname);

            if(!address.getHostName().equals("localhost")) {
                MoltenLogger.Log(Constants.INTERNET_CONNECTION_PRESENT);
                return true;
            }
            MoltenLogger.Log(Constants.INTERNET_CONNECTION_FAILED);
            return false;
        } catch (UnknownHostException e) {
            MoltenLogger.Log(Constants.INTERNET_CONNECTION_FAILED);
               return false;
        }
    }

    public String getTestHostname() {
        return testHostname;
    }

    public void setTestHostname(String testHostname) {
        this.testHostname = testHostname;
    }
}
